/******************************************************************************
 * @File name   :      MaintainMemberController.java
 *
 * @Author      :      ANMA
 *
 * @Date        :      Jul 20, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * ----------------------------------------------------------------------------
 * Date                         Who         Version         Comments
 * Jul 20, 2015 7:03:02 PM    ANMA      1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.member.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.cap.bts.commonfunc.common.util.UserUtil;
import com.cap.bts.crmdp.dealer.member.constants.MaintainMemberConstants;
import com.cap.bts.crmdp.dealer.member.domain.CarPlateInfo;
import com.cap.bts.crmdp.dealer.member.domain.MemberInfo;
import com.cap.bts.crmdp.dealer.member.dto.MemberQueryDto;
import com.cap.bts.crmdp.dealer.member.dto.MemberResultsDto;
import com.cap.bts.crmdp.dealer.member.dto.MemberSubmitDto;
import com.cap.bts.crmdp.dealer.member.dto.WechatMemberQueryDto;
import com.cap.bts.crmdp.dealer.member.service.IMaintainCarPlateService;
import com.cap.bts.crmdp.dealer.member.service.IMaintainMemberService;
import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.crmdp.dealer.purchase.service.IMemberPackageService;
import com.cap.bts.framework.codemapping.domain.CodeInfo;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.constant.CommonConstants;
import com.cap.bts.framework.common.controller.BaseController;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.util.Utils;
import com.cap.bts.framework.paramgnt.service.ISystemParaManagement;
import com.cap.bts.framework.usermgnt.domain.UserRole;
import com.cap.bts.framework.usermgnt.service.IUserMgntService;

/**
 * 
 */
@Controller
@RequestMapping(value = "/crmdp/dealer/member")
public class MaintainMemberController extends BaseController{

	static final String GETMAINTAINMEMBERPAGE = "getMaintainMemberPage";
	static final String SUBMITDTO = "submitDto";
	static final String DTO = "dto";
	private static final Logger LOGGER = Logger.getLogger(MaintainMemberController.class);
	@Autowired
	IMaintainMemberService maintainMemberService;
	@Autowired
	IMaintainCarPlateService maintainCarPlateService;
	@Autowired
	IMemberPackageService memberPackageService;
	@Autowired
    private ICodeMapping codeMapping;
	@Autowired
    private ISystemParaManagement systemParaManagement;
	@Autowired
    private IUserMgntService userService;
	/**
	 * 会员查询首页
	 * 
	 * @Date : 2015-7-15
	 * @param dto
	 *            MemberQueryDto
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/toPurchaseUser")
	public String toPurchaseMemberPage(MemberSubmitDto dto, Model model, HttpServletRequest request) throws AppException {
		model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
		model.addAttribute(SUBMITDTO, dto);
		return "/member/purchaseUserInfo";
	}
	
	/**
	 * 会员查询首页
	 * 
	 * @Date : 2015-7-15
	 * @param dto
	 *            MemberQueryDto
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/toSearchMemberPage")
	public String toSearchMemberPage(MemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
		model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
		model.addAttribute(DTO, dto);
		return "/member/maintainMember";
	}

	/**
	 * 维护经销商查询页
	 * 
	 * @Date : 2015-7-20
	 * @param dto
	 *            MemberQueryDto
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMaintainMemberPage")
	public String getMaintainMemberPage(MemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {

		List<MemberResultsDto> memberResultsDtoList = new ArrayList<MemberResultsDto>();
		if (dto != null) {
			dto.setId(null);
			dto.setMemberName(null);
			dto.setMobilePhone(null);
			dto.setCarPlate(null);
			dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
			memberResultsDtoList = maintainMemberService.getMemberAndCarPlateByDto(dto);
			model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
		}
		request.getSession().setAttribute(GETMAINTAINMEMBERPAGE, dto);
		model.addAttribute("memberResultsDtoList", memberResultsDtoList);
		model.addAttribute(DTO, dto);
		return "/member/maintainMember";
	}
	
	@RequestMapping(value = "/getMaintainMemberPageFromWechat")
	public String getMaintainMemberPageFromWechat(WechatMemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
	    
	    List<WechatMemberQueryDto> memberResultsDtoList = new ArrayList<WechatMemberQueryDto>();
	    if (dto != null) {
	        dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
	        memberResultsDtoList = maintainMemberService.getWechatMemberOrCarPlateByDto(dto);
	        model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
	    }
	    model.addAttribute("memberResultsDtoList", memberResultsDtoList);
	    model.addAttribute("queryDto", dto);
	    model.addAttribute("fromSearch", "fromSearch");
	    return "/member/selectMemberFromWechat";
	}
	
	@RequestMapping(value = "/checkMaintainMemberPageFromWechat")
    @ResponseBody
    public boolean checkMaintainMemberPageFromWechat(WechatMemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
        
        if (dto != null) {
            dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
            boolean isExits = maintainMemberService.checkWechatMemberOrCarPlateByDto(dto.getMobilePhoneOrCarPlate());
            if(isExits){
                return true;
            }
        }
        return false;
    }
	
	@RequestMapping(value = "/saveMemberFromWechat")
	@ResponseBody
    public boolean saveMemberFromWechat(WechatMemberQueryDto dto, Model model, HttpServletRequest request) throws AppException {
	    if(dto != null){
	        String membermobile = dto.getMobilePhone();
	        String carPlate = dto.getCarPlate();
	        //在member表中查询是否有该账户
	        MemberInfo memberInfo = maintainMemberService.getMemberByDealerIdMemberIdMobilePhone(UserUtil.getCurrentUser().getDealerId(), membermobile);
	        if(memberInfo != null){
	            //存在， 添加carPlate表，关联该memberId
	            CarPlateInfo carPlateInfo = new CarPlateInfo();
	            carPlateInfo.setCarPlate(carPlate);
	            carPlateInfo.setMemberId(memberInfo.getId());
	            carPlateInfo.setRegisterDate(new Date());
	            carPlateInfo.setMemberStatus(MaintainMemberConstants.NO_ACTIVE);
	            maintainCarPlateService.saveWechatCarPlateInfo(carPlateInfo);
	            return true;
	        }else{
	            //不存在，member、carPlate都添加
	            MemberInfo member = maintainMemberService.saveMemberByDto(dto);
	            if(member != null){
	                MemberInfo newMember = maintainMemberService.getMemberByDealerIdMemberIdMobilePhone(UserUtil.getCurrentUser().getDealerId(), membermobile);
	                if(newMember != null){
	                    CarPlateInfo carPlateInfo = new CarPlateInfo();
	                    carPlateInfo.setCarPlate(carPlate);
	                    carPlateInfo.setMemberId(newMember.getId());
	                    carPlateInfo.setRegisterDate(new Date());
	                    carPlateInfo.setMemberStatus(MaintainMemberConstants.NO_ACTIVE);
	                    maintainCarPlateService.saveWechatCarPlateInfo(carPlateInfo);
	                    return true;
	                }
	            }
	        }
	    }
	    return false;
	}
	
	/**
	 * 
	 * @Date        :      Sep 15, 2015
	 * @param dto
	 * @param model
	 * @param request
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/redirectMaintainMemberPage")
	public String redirectMaintainMemberPage(Model model, HttpServletRequest request) throws AppException {
	    MemberQueryDto memberQueryDto = (MemberQueryDto) request.getSession().getAttribute(GETMAINTAINMEMBERPAGE);
	    return this.getMaintainMemberPage(memberQueryDto, model, request);
	}
	
	/**
	 * 转向新增经销商页面
	 * 
	 * @Date : 2015-7-22
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws AppException
	 */
	@RequestMapping(value = "/toCreateMemberPage")
	public String getcreateMemberPage(Model model, HttpServletRequest request) throws AppException {

		MemberSubmitDto memberSubmitDto = new MemberSubmitDto();
		model.addAttribute(SUBMITDTO, memberSubmitDto);
		return "/member/createMember";
	}
	
	@RequestMapping(value = "/toSelectMemberFromWechatPage")
	public String selectMemberFromWechatPage(Model model, HttpServletRequest request) throws AppException {
	    WechatMemberQueryDto dto = new WechatMemberQueryDto();
	    model.addAttribute(CommonConstants.REQUEST_ATTRIBUTE_PAGER, dto.getPager());
	    model.addAttribute("queryDto", dto);
	    model.addAttribute("fromSearch", null);
	    return "/member/selectMemberFromWechat";
	}
	
	/**
	 * @Date        :      2015年8月4日
	 * @param memberSubmitDto
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/checkMemberMobile")
    @ResponseBody
    public boolean checkMemberMobile(MemberSubmitDto memberSubmitDto) throws AppException {
        if(memberSubmitDto != null){
            List<MemberResultsDto> memberResultsDtoList = getMemberByMobilePhoneOrMemberId(memberSubmitDto);
            if(memberResultsDtoList != null 
                && !memberResultsDtoList.isEmpty() 
                && memberResultsDtoList.get(0).getMobilePhone().equals(memberSubmitDto.getMobilePhone())){
                    return true;
                }
        }
        return false;
	}
	
	
	/**
	 * @Date        :      2015年8月4日
	 * @param memberSubmitDto
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/checkCarPlate")
	@ResponseBody
	public boolean checkCarPlate(MemberSubmitDto memberSubmitDto) throws AppException {
	    if(memberSubmitDto != null){
	        List<MemberResultsDto> memberResultsDtoList = getMemberByCarPlate(memberSubmitDto);
	        if(memberResultsDtoList != null 
	            && !memberResultsDtoList.isEmpty()){
                    return true;
	        }
	    }
	    return false;
	}
	/**
	 * 转向新增经销商页面
	 * 
	 * @Date : 2015-7-22
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws AppException
	 */
	@RequestMapping(value = "/checkMemberExisted")
	@ResponseBody
	public MemberResultsDto checkMemberExisted(MemberSubmitDto memberSubmitDto, Model model, HttpServletRequest request) throws AppException {
		
		MemberResultsDto rsDto = null;
		if(memberSubmitDto != null){
			//String memberName = memberSubmitDto.getMemberName()
			String carPlate = memberSubmitDto.getCarPlate();
			String mobilePhone = memberSubmitDto.getMobilePhone();
			
			List<MemberResultsDto> memberResultsDtoList = getMemberByMobilePhoneOrMemberId(memberSubmitDto);

			//
			// 老客户
			//先判断是否是老客户，再按照次客户ID去查找绑定的车牌
			if(memberResultsDtoList != null && !memberResultsDtoList.isEmpty()){
				
				MemberResultsDto member = memberResultsDtoList.get(0);
				Long memberId =  member.getId();
				
				List<CarPlateInfo> 	carPlateInfoList =  maintainCarPlateService.getCarPlateInfoListByCarPlateOrMemberId("",memberId);

				if(carPlateInfoList != null && !carPlateInfoList.isEmpty()){
					for (int i = 0; i < carPlateInfoList.size(); i++) {
						CarPlateInfo cinfo = carPlateInfoList.get(i);
						if(carPlate.equalsIgnoreCase(cinfo.getCarPlate())){
							//
							//老客户+老车， 提醒是否更新客户姓名
							//
							//手机号+车牌号已经注册过
							rsDto = new MemberResultsDto();
							rsDto.setMobilePhone(mobilePhone);
							rsDto.setCarPlate(carPlate);
							return rsDto;
						}else{
							//
							//老客户+新车，提醒是否 为老客户添加 客户-车牌关系
							//
							//提示客户此手机已注册，是否为客户增加新的车辆信息
							rsDto = new MemberResultsDto();
							rsDto.setMemberName(member.getMemberName());
							rsDto.setMobilePhone(mobilePhone);
							return rsDto;
						}
					}
				}
			}else{

				//
				// 新客户+老车， 提示用户车辆已关联其他客户，是否再关联当前客户
				//
				List<CarPlateInfo> 	carPlateInfoList =  maintainCarPlateService.getCarPlateInfoListByCarPlateOrMemberId(carPlate,null);
				if(carPlateInfoList != null && !carPlateInfoList.isEmpty()){
					for (int i = 0; i < carPlateInfoList.size(); i++) {
						CarPlateInfo cinfo = carPlateInfoList.get(i);

						MemberQueryDto querydto = new MemberQueryDto();
						querydto.setId(cinfo.getMemberId());

						List<MemberResultsDto> rslist = maintainMemberService.getMemberByMobilePhoneOrMemberId(querydto);
						//
						//获取与当前经销商对应的会员的车牌号做比较
						//
						if(rslist!=null && !rslist.isEmpty() 
						    && rslist.get(0).getDealerId().equals(UserUtil.getCurrentUser().getDealerId()) 
						    && carPlate.equalsIgnoreCase(cinfo.getCarPlate())){
								rsDto = new MemberResultsDto();
								rsDto.setCarPlate(carPlate);
								return rsDto;
						}
					}
				}else{
					//
					// 新客户直接新建客户和 客户-车牌关系
					//
					return new MemberResultsDto(); 
					
				}
			}
		}
		return new MemberResultsDto();
	}

	/**
	 * 
	 * 
	 * @Date : 2015-7-22
	 * @param MemberSubmitDto
	 *            memberSubmitDto
	 * @throws AppException
	 */
	public List<MemberResultsDto> getMemberByMobilePhoneOrMemberId(MemberSubmitDto memberSubmitDto) throws AppException {
		List<MemberResultsDto> memberResultsDtoList = null;
		MemberQueryDto dto = new MemberQueryDto();
		dto.setId(memberSubmitDto.getId());
		dto.setMobilePhone(memberSubmitDto.getMobilePhone());
		dto.setCarPlate(memberSubmitDto.getCarPlate());
		dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
		memberResultsDtoList = maintainMemberService.getMemberByMobilePhoneOrMemberId(dto);
		return memberResultsDtoList;
	}
	
	public List<MemberResultsDto> getMemberByCarPlate(MemberSubmitDto memberSubmitDto) throws AppException {
	    List<MemberResultsDto> memberResultsDtoList = null;
	    MemberQueryDto dto = new MemberQueryDto();
	    dto.setCarPlate(memberSubmitDto.getCarPlate());
	    memberResultsDtoList = maintainMemberService.getMemberByCarPlateOfThisDealer(dto);
//	    memberResultsDtoList = maintainMemberService.getMemberByCarPlate(dto);
	    return memberResultsDtoList;
	}
	
	/**
	 * 
	 * 
	 * @Date : 2015-7-22
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws AppException
	 */
	@RequestMapping(value = "/toAddCarPlateForMember")
	@ResponseBody
	public boolean toAddCarPlateForMember(MemberSubmitDto submitDto, Model model, HttpServletRequest request) throws AppException {
		MemberResultsDto memberResultsDto = null;
		List<MemberResultsDto> memberResultsDtoList = getMemberByMobilePhoneOrMemberId(submitDto);
		if(memberResultsDtoList != null && !memberResultsDtoList.isEmpty()){
			memberResultsDto = memberResultsDtoList.get(0);
			memberResultsDto.setCarPlate(submitDto.getCarPlate());
			maintainCarPlateService.saveCarPlateInfo(memberResultsDto);
			return true;
		}
		return false;
	}
	
    @RequestMapping(value = "/checkCarPlateForMember")
    @ResponseBody
    public MemberResultsDto checkCarPlateForMember(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            List<CarPlateInfo> carPlateInfo = maintainCarPlateService.getCarPlateInfoListByCarPlate(resultsDto.getCarPlate());
            if(carPlateInfo != null && !carPlateInfo.isEmpty()){
                resultsDto.setCarplatesExist(true);
                resultsDto.setSuccess(false);
                return resultsDto;
            }else{
                resultsDto.setCarplatesExist(false);
                resultsDto.setSuccess(true);
                return resultsDto;
            }
        } catch (Exception e) {
            throw new AppException("checkCarPlateForMember", e);
        }
    }
    
    @RequestMapping(value = "/checkCarPlateForThisMember")
    @ResponseBody
    public MemberResultsDto checkCarPlateForThisMember(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            List<CarPlateInfo> carPlateInfo = maintainCarPlateService.getCarPlateInfoListByCarPlateAndMemberId(resultsDto.getCarPlate(),resultsDto.getId());
            if(carPlateInfo != null && !carPlateInfo.isEmpty()){
                resultsDto.setCarplateOfMemberExist(true);
            }else{
                resultsDto.setCarplateOfMemberExist(false);
                resultsDto.setSuccess(true);
            }
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("checkCarPlateForThisMember", e);
        }
    }

	/**
	 * @Date        :      2015年8月5日
	 * @param submitDto
	 * @param model
	 * @param request
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/addCarPlateForMember")
	@ResponseBody
	public MemberResultsDto addCarPlateForMember(@RequestBody String jsonStr) throws AppException {
	    try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            maintainCarPlateService.saveCarPlateInfo(resultsDto);
            resultsDto.setSuccess(true);
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("addCarPlateForMember", e);
        }
	}
	
	/**
	 * @Date        :      2015年8月6日
	 * @param mobilePhone
	 * @param memberName
	 * @param carPlate
	 * @param id
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/removeCarPlateForMember")
	@ResponseBody
	public MemberResultsDto removeCarPlateForMember(@RequestBody String jsonStr) throws AppException {
	    try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            boolean isExist = maintainCarPlateService.existUsingPackage(resultsDto.getId());
            if(!isExist){
                maintainCarPlateService.removeCarPlateInfo(resultsDto.getId());
                resultsDto.setSuccess(true);
            }else{
                resultsDto.setExistUsingPackage(true);
                resultsDto.setSuccess(false);
            }
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("removeCarPlateForMember", e);
        }
	}
	
	
	/**
	 * @Date        :      2015年8月6日
	 * @param jsonStr
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/updateMemberName")
	@ResponseBody
	public MemberResultsDto updateMemberName(@RequestBody String jsonStr) throws AppException {
	    try {
	        MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            boolean issuccess = maintainMemberService.updateMemberNameByMemberResultsDto(resultsDto);
            if(issuccess){
                resultsDto.setSuccess(true);
            }
            return resultsDto;
	    } catch (Exception e) {
	        throw new AppException("updateMemberName", e);
	    }
	}
	/**
	 * 
	 * 
	 * @Date : 2015-7-22
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws AppException
	 */
	@RequestMapping(value = "/toUpdateMemberName")
	@ResponseBody
	public MemberResultsDto toUpdateMemberName(MemberSubmitDto submitDto, Model model, HttpServletRequest request) throws AppException {

		MemberResultsDto responseDto = null;
		if(submitDto == null){
			return null;
		}
		String memberName = submitDto.getMemberName();
		
		MemberQueryDto dto = new MemberQueryDto();
		dto.setMobilePhone(submitDto.getMobilePhone());
		dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
		MemberResultsDto rsDto = maintainMemberService.getMemberByMobilePhoneOrMemberId(dto).get(0);
		Long memberId = rsDto.getId();

		if(memberName.equals(rsDto.getMemberName())){
			responseDto = new MemberResultsDto();
			responseDto.setMemberName(memberName);
			return responseDto;
		}

		MemberSubmitDto memberSubmitDto = new MemberSubmitDto();
		model.addAttribute(SUBMITDTO, memberSubmitDto);

		MemberInfo info = new MemberInfo();
		info.setMobilePhone(rsDto.getMobilePhone());
		info.setMemberName(memberName);
		info.setId(memberId);

		maintainMemberService.getUpdateMemberName(info);
		return new MemberResultsDto();
	}
	/**
	 * 转向新增经销商页面
	 * 
	 * @Date : 2015-7-22
	 * @param model
	 *            Model
	 * @param request
	 *            HttpServletRequest
	 * @return String
	 * @throws AppException
	 */
	@RequestMapping(value = "/submitToSaveMember")
	@ResponseBody
	public MemberResultsDto submitToSaveMember(MemberSubmitDto memberSubmitDto, Model model, HttpServletRequest request) throws AppException {

		MemberResultsDto rsDto = null;
		List<MemberResultsDto> memberResultsDtoList = getMemberByMobilePhoneOrMemberId(memberSubmitDto);
		if(memberResultsDtoList != null && !memberResultsDtoList.isEmpty()){
			rsDto = new MemberResultsDto();
			rsDto.setMemberName(memberSubmitDto.getMemberName());
			return rsDto;
		}
		
		model.addAttribute(SUBMITDTO, memberSubmitDto);

		String mobilePhone = request.getParameter("mobilePhone");
		String memberName = request.getParameter("memberName");
		String carPlate = request.getParameter("carPlate");
		if (Utils.notEmpty(mobilePhone) && Utils.notEmpty(memberName) && Utils.notEmpty(carPlate)) {
			memberSubmitDto.setDealerId(UserUtil.getCurrentUser().getDealerId());
			rsDto = maintainMemberService.getCreateMemberByDto(memberSubmitDto);
			return rsDto;
		}else{
			return null;
		}
	}
	
	/**
	 * @Date        :      2015年9月2日
	 * @param jsonStr
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/updateMemberMobile")
    @ResponseBody
    public MemberResultsDto updateMemberMobile(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            boolean issuccess = maintainMemberService.updateMemberMobileByMemberResultsDto(resultsDto);
            if(issuccess){
                resultsDto.setSuccess(true);
            }
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("updateMemberName", e);
        }
    }
	
	/**
	 * @Date        :      2015年9月2日
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/ischeckoperationpwd")
	@ResponseBody
	public boolean ischeckoperationpwd() throws AppException {
            try {
                String isSendMessage = systemParaManagement.getSysParameter("PREFIX_OPERATIONPWD_SETTING", "IS_NEED_OPERATIONPWD");
                UserRole userRole = userService.getUserRoleByUserCodeAndRoleCode(
                    UserUtil.getCurrentUser().getUserCode(), MaintainMemberConstants.SHOPKEEPER,0);
                if(Utils.notEmpty(isSendMessage) && isSendMessage.equals(MaintainMemberConstants.IS_NEED_OPERATIONPWD_TRUE)
                    && userRole == null){
                    return true;
                }
                return false;
            } catch (Exception e) {
                LOGGER.error("ischeckoperationpwd!",e);
                throw new AppException("Exception: Failed to check operationpwd",e);
            }
	}
	
	@RequestMapping(value = "/chackMemberMobile")
    @ResponseBody
    public MemberResultsDto chackMemberMobile(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            MemberQueryDto dto = new MemberQueryDto();
            MemberResultsDto member = new MemberResultsDto();
            dto.setMobilePhone(resultsDto.getMobilePhone());
            dto.setDealerId(UserUtil.getCurrentUser().getDealerId());
            List<MemberResultsDto> list = maintainMemberService.getMemberByMobilePhoneOrMemberId(dto);
            if(list != null && !list.isEmpty()){
                member = list.get(0);
                member.setExistMember(true);
            }
            return member;
        } catch (Exception e) {
            throw new AppException("chackMemberMobile", e);
        }
    }
	@RequestMapping(value = "/mergeMemberInfo")
    @ResponseBody
    @Transactional
    public MemberResultsDto mergeMemberInfo(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            MemberQueryDto newDto = new MemberQueryDto();
            newDto.setMobilePhone(resultsDto.getMobilePhone());
            newDto.setDealerId(UserUtil.getCurrentUser().getDealerId());
            MemberResultsDto newMember = new MemberResultsDto();
            List<MemberResultsDto> newMemberList = maintainMemberService.getMemberByMobilePhoneOrMemberId(newDto);
            if(newMemberList != null && !newMemberList.isEmpty()){
                newMember = newMemberList.get(0);
                List<CarPlateInfo> carPlateInfoList = maintainCarPlateService.getCarPlateInfoListByCarPlateOrMemberId(null, resultsDto.getId());
                if(carPlateInfoList != null && !carPlateInfoList.isEmpty()){
                    for(int i=0;i<carPlateInfoList.size();i++){
                        carPlateInfoList.get(i).setMemberId(newMember.getId());
                        maintainCarPlateService.updateCarPlateByCarPlateInfo(carPlateInfoList.get(i),newMember.getId());
                    }
                }
                List<MemberPackageInfo> memberPackageInfo = memberPackageService.getMemberPackageInfoByActiveDealerIdAndActiveMemberId(
                    UserUtil.getCurrentUser().getDealerId(), resultsDto.getId());
                if(memberPackageInfo != null && !memberPackageInfo.isEmpty()){
                    for(int i=0;i<memberPackageInfo.size();i++){
                        memberPackageService.updateMemberPackageInfoByMemberPackageInfo(memberPackageInfo.get(i),newMember.getId());
                    }
                }
                maintainMemberService.removeMemberById(resultsDto.getId());
            }
            return newMember;
        } catch (Exception e) {
            throw new AppException("mergeMemberInfoException", e);
        }
    }
	
	/**
	 * @Date        :      2015年9月6日
	 * @param jsonStr
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/updateCarPlateForMember")
    @ResponseBody
    public MemberResultsDto updateCarPlateForMember(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            CarPlateInfo carPlate = maintainCarPlateService.getCarPlateInfoById(resultsDto.getId());
            if(carPlate != null){
                if(maintainCarPlateService.updateCarPlateForMember(resultsDto, carPlate.getUpdateTimes())){
                    resultsDto.setUpdateCarplateSuccess(true);
                }else{
                    resultsDto.setUpdateCarplateSuccess(false);
                }
            }
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("updateCarPlateForMember", e);
        }
    }
	
	@RequestMapping(value = "/updateCarPlateWithoutPackage")
    @ResponseBody
    public MemberResultsDto updateCarPlateWithoutPackage(@RequestBody String jsonStr) throws AppException {
        try {
            MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
            CarPlateInfo carPlate = maintainCarPlateService.getCarPlateInfoById(resultsDto.getId());
            if(carPlate != null){
                carPlate.setCarPlate(resultsDto.getCarPlate());
                maintainCarPlateService.updateCarPlate(carPlate);
                resultsDto.setUpdateCarplateSuccess(true);
            }
            return resultsDto;
        } catch (Exception e) {
            throw new AppException("updateCarPlateForMember", e);
        }
    }
	
	/**
	 * 
	 * @Date        :      Sep 15, 2015
	 * @param jsonStr
	 * @return
	 * @throws AppException
	 */
	@RequestMapping(value = "/checkUpdateCarPlateTimes")
	@ResponseBody
	public MemberResultsDto checkUpdateCarPlateTimes(@RequestBody String jsonStr) throws AppException {
	    try {
	        MemberResultsDto resultsDto = JSON.parseObject(jsonStr,MemberResultsDto.class);
	        CarPlateInfo carPlate = maintainCarPlateService.getCarPlateInfoById(resultsDto.getId());
	        CodeInfo codeInfo = codeMapping.getCodeInfoByCode(MaintainMemberConstants.UPDATE_TIMES);
	        if(carPlate != null && codeInfo != null && carPlate.getUpdateTimes() < Integer.parseInt(codeInfo.getValue())){
	            resultsDto.setGreaterThanUpdateCarplateTimes(false);
	        }else{
	            resultsDto.setGreaterThanUpdateCarplateTimes(true);
	        }
	        return resultsDto;
	    } catch (Exception e) {
	        throw new AppException("updateCarPlateForMember", e);
	    }
	}
	
	@RequestMapping(value = "/checkCarPlateRules")
	@ResponseBody
	public boolean checkCarPlateRules(MemberSubmitDto memberSubmitDto) throws AppException {
		if(memberSubmitDto != null){
			String carPlate = memberSubmitDto.getCarPlate();
			String regEx1 = codeMapping.getCodeValueByCode("regex", "regex");
			Pattern p1 = Pattern.compile(regEx1); 
			Matcher m1 = p1.matcher(carPlate);
			boolean rs1 = m1.matches();
			return rs1;
		}
	    return false;
	}
	
	@RequestMapping(value = "/checkCarPlateRule")
	@ResponseBody
	public boolean checkCarPlateRule(@RequestBody String jsonStr) throws AppException {
		MemberSubmitDto memberSubmitDto = JSON.parseObject(jsonStr,MemberSubmitDto.class);
		if(memberSubmitDto != null){
			String carPlate = memberSubmitDto.getCarPlate();
			String regEx1 = codeMapping.getCodeValueByCode("regex", "regex");
			Pattern p1 = Pattern.compile(regEx1);
			Matcher m1 = p1.matcher(carPlate);
			boolean rs1 = m1.matches();
			return rs1;
		}
		return false;
	}
}
